/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package mytest.testPropertyPanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.util.Collection;
import java.util.logging.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.propertysheet.PropertyPanel;
import org.openide.nodes.BeanNode;
import org.openide.nodes.Node;
import org.openide.nodes.Node.Property;
import org.openide.nodes.Node.PropertySet;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Utilities;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//mytest.testPropertyPanel//CarProp//EN",
autostore = false)
@TopComponent.Description(preferredID = "CarPropTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "properties", openAtStartup = true)
@ActionID(category = "Window", id = "mytest.testPropertyPanel.CarPropTopComponent")
@ActionReference(path = "Menu/Window" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_CarPropAction",
preferredID = "CarPropTopComponent")
public final class CarPropTopComponent extends TopComponent implements LookupListener {

    public CarPropTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(CarPropTopComponent.class, "CTL_CarPropTopComponent"));
        setToolTipText(NbBundle.getMessage(CarPropTopComponent.class, "HINT_CarPropTopComponent"));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
    private GridBagConstraints gbc = new GridBagConstraints();
    private Lookup.Result<BeanNode> result = null;

    @Override
    public void resultChanged(LookupEvent le) {
        Collection<? extends BeanNode> nodes = result.allInstances();
        if (!nodes.isEmpty()) {
            for (Node node : nodes) {
                PropertySet[] sets = node.getPropertySets();
                for (PropertySet propertySet : sets) {
                    Property[] props = propertySet.getProperties();
                    removeAll();
                    for (Property property : props) {
                        gbc.gridwidth = GridBagConstraints.REMAINDER;
                        gbc.fill = GridBagConstraints.BOTH;
                        property.setValue("suppressCustomEditor", true);
                        if (property.getValueType() == String.class) {
                            MyPanel cpp = new MyPanel(property);

                            add(cpp, gbc);
                        }

                        open();
                        requestActive();
                    }
                }
            }
        }
    }

    class MyPanel extends PropertyPanel {

        Color color;

        public MyPanel(Property property) {
            super(property);
            this.color = Color.CYAN;
            for (Component c:getComponents()){
                
            }
        }

        @Override
        public void paintComponent(Graphics g) {
            int width = getWidth();
            int height = getHeight();
            g.setColor(color);
            g.drawOval(0, 0, width, height);
        }

        @Override
        public void paint(Graphics g) {
            int width = getWidth();
            int height = getHeight();
            g.setColor(color);
            g.drawOval(0, 0, width, height);
        }
    }

    @Override
    public void componentOpened() {
        result = Utilities.actionsGlobalContext().lookupResult(BeanNode.class);
        result.addLookupListener(this);
        resultChanged(new LookupEvent(result));
        Logger.getLogger(this.getClass().getName()).info("Opened !");
    }

    @Override
    public void componentClosed() {
        result.removeLookupListener(this);
    }
}
